<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018-10-17
 * Time: 9:25
 */
namespace app\api\model;
use app\api\model\GetBank;
use app\api\model\Login;
use think\Db;
use think\Request;
use think\Validate;

class Index
{
    /***
     * 获取当前用户排名
     * @param $user
     * @param $user_list
     * @return int|string
     */
    static public function paih($user,$user_list)
    {
        foreach ($user_list as $k=>$v)
        {
            if($v["id"]==$user["id"]){
                $current_user = $k+1;
            }
        }
        return $current_user;
    }

    /***处理做题状态
     * @param $ti 当前题目
     * @param $user 用户信息
     * @param $right_key 用户做出的选项
     * @return array
     */
    static public function timu($ti,$user,$right_key,$end_time)
    {
        $user['answer_number'][] = $ti["id"];
        if($ti['right_key']==$right_key){
            $user['correct'] = $user['correct']+1;
            $user['experience'] = (int)$user['experience']+(int)$ti['experience'];
            $user['coin']       = (int)$user['coin']+(int)$ti['coin'];
            $bol = Db::name('grade')->where('number',((int)$user['grade']+1))->find();
            if($user['experience']>=$bol['grade_experience']){
                $user['grade'] = (int)$user['grade']+1;
                $user['experience'] = (int)$user['experience']-(int)$bol['grade_experience'];
                if($end_time){
                    $user["end_time"] = (int)$end_time;
                }
            }
            if(Db::name('user')->where('id',$user['id'])->json(["answer_number"])->update($user)){
                //答对了，前台回调上面的出题方法
                return ["code"=>200,"msg"=>"正确"];
            }else{
                return ["code"=>401,"msg"=>"修改失败"];
            }
        }else{
            //写入错题集
            $data["user_id"] = (int)$user["id"];
            $data["tables_id"] = (int)$ti["id"];
            $data["create_time"] = time();
            if(!Db::name("error")->insert($data)){
                return ["code"=>401,"msg"=>"错题录入失败"];
            }
            $coin = $user["coin"]-1;
            //修改做过题的个数
            if(!Db::name("user")
                ->where("id",$user["id"])
                ->json(['answer_number'])
                ->update(["answer_number"=>$user["answer_number"],"coin"=>$coin,"end_time"=>$end_time])){
                return ["code"=>401,"msg"=>"修改失败"];
            }
            return ["code"=>201,"msg"=>$ti];
        }
    }

    /***
     * 获取杂项限制
     * @param $system
     * @return array|bool|null|\PDOStatement|string|\think\Model
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function sys($system)
    {
        if(!$system){
            return false;
        }
        $val = Db::name("system")->where("identification",$system)->find();
        return $val;
    }

    /***
     * 考试资格（是否有足够金币）
     * @param $user
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    static public function kszg($user)
    {
        //考试每一道题扣除的金币
        $kaoshikouchudejinbi = self::sys("kaoshikouchudejinbi")["system_value"];
        //每次试卷的题目数量
        $shiti_num = self::sys("shiti")["system_value"];
        $ksfy = $kaoshikouchudejinbi * $shiti_num;
        if($ksfy > $user["coin"]){
            return ["code"=>401,"msg"=>"本次考试需要".$ksfy."金币，您的余额不足，请充值后再来"];
        }
    }

    /***考试结算
     * @param $user
     * @return bool
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    static public function Ksjiesuan($user)
    {
        $user["answer_number"] = null;
        $user["fraction"] = $user["correct"];
        $user["end_time"] = null;
        $user["correct"] = null;
        if(Db::name("user")->where("id",$user["id"])->update($user)){
            return true;
        }else{
            return false;
        }

    }


}